<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="12">
          <a-col :md="4" :sm="8">
            <a-form-item
              label="手机号"
              :maxLength="50">
              <a-input placeholder="请输入手机号" v-model="queryParam.mobile"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="8">
            <a-form-item
              label="身份证号"
              :maxLength="50">
              <a-input placeholder="请输入身份证号" v-model="queryParam.idcard"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="8">
            <a-form-item
              label="姓名"
              :maxLength="50">
              <a-input placeholder="请输入姓名" v-model="queryParam.name"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item
              label="注册日期范围"
              :maxLength="50">
              <a-range-picker @change="onChange" v-model="queryParam.timeinfo" valueFormat="YYYY-MM-DD"/>
            </a-form-item>
          </a-col>
          <a-col :md="5" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="$refs.table.refresh(true)" icon="search">查询</a-button>
              <a-button
                @click="() => {this.$objToReset(queryParam),$refs.table.refresh(true)}"
                icon="reload"
                style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
      <s-table
        ref="table"
        rowKey="id"
        size="middle"
        :scroll="{ x: 1300 }"
        :columns="columns"
        :data="loadData">
        <template slot="action" slot-scope="text, record">
          <!--<a @click="handelView(record)" v-wx-role="804">
            <a-icon type="eye"/>
            提现记录</a>
          <a-divider type="vertical"/>
          <a @click="handelViewBalance(record)" v-wx-role="803">
            <a-icon type="eye"/>
            佣金记录</a>-->
          <a @click="handleEdit(record)" >
            <a-icon type="edit"/>编辑</a>
          <a-divider type="vertical"/>
          <a-popconfirm title="确定删除吗?" @confirm="() =>handleDelete(record.id)">
            <a>删除</a>
          </a-popconfirm>
        </template>
        <template slot="avatarslot" slot-scope="text, record">
          <div class="anty-img-wrap">
            <!--            <a-avatar shape="square" :src="getAvatarView(record.avatar)" icon="user"/>-->
            {{ record.nickname }}
          </div>
        </template>
      </s-table>

        <user-edit ref="modalEdit" @ok="modalEditOk"></user-edit>
    </div>
  </a-card>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import { deleteAction, postAction } from '@/api/manage'
import { mixin } from '@/utils/mixin'
import WxFilterDepart from '@/components/WxFilterDepart/WxFilterDepart'
import store from '@/store'
import UserEdit from "./UserEdit";

export default {
  name: 'UserList',
  mixins: [mixin],
  components: {
      UserEdit,
    STable,
    Ellipsis,
    WxFilterDepart
  },
  data () {
    return {
      visible: false,
      // 查询条件
      queryParam: {
        mobile: '',
        timeinfo: [],
        times: '',
        idcard: '',
        name: ''
      },
      // 表头
      // 如果需要字典，那么需要在columns列中加入 customRender: (text) => this.$sysDictFilter(text, 'xxxx')xxxx 为dataIndex
      columns: [
        {
          title: '昵称',
          dataIndex: 'avatar',
          scopedSlots: { customRender: 'avatarslot' }
        },
        {
          title: '手机号',
          dataIndex: 'mobile',
          customRender: (text, record) => (text || '-')
        },
        {
          title: '姓名',
          dataIndex: 'name',
          customRender: (text, record) => (text || '-')
        },
        {
          title: '身份证号',
          dataIndex: 'idcard',
          customRender: (text, record) => (text || '-')
        },
        {
          title: '民族',
          dataIndex: 'nation',
          customRender: (text, record) => (text || '-')
        },
        {
          title: '城市',
          dataIndex: 'region',
          customRender: (text, record) => (text || '-')
        },
        {
          title: '详细地址',
          dataIndex: 'address',
          customRender: (text, record) => (text || '-')
        },
        {
          title: '创建时间',
          dataIndex: 'createTime'
        },
         {
           title: '操作',
          dataIndex: 'action',
           align: 'center',
          scopedSlots: {customRender: 'action'}
         }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        console.log('loadData.parameter', parameter)
        return postAction(this.url.list, Object.assign(parameter, this.queryParam))
            .then(res => {
              return res.result
            })
      },
      selectedRowKeys: [],
      options: {
        alert: {
          clear: () => {
            this.selectedRowKeys = []
          }
        },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange,
          type: 'checkbox'
        }
      },
      url: {
        list: '/system/user/list',
        delete: '/system/user/delete'
      }
    }
  },
  computed: {},
  methods: {
    // 图像处理
    getAvatarView: function (avatar) {
      return this.$getFileAccessHttpUrl(avatar)
    },
    // 用户编辑
    handleEdit (record) {
      this.$refs.modalEdit.edit(record)
    },
    searchQuery () {
      this.$refs.table.refresh(true)
    },
    // 保存刷新
    modalEditOk () {
      this.$refs.table.refresh()
    },
    onChange (value, dateString) {
      console.log(dateString[0], dateString[1])
      this.queryParam.times = dateString[0] + '~' + dateString[1]
    },
      handleDelete (record) {
          if (!record) {
              // 如果没有传入，那么批量删除
              record = this.selectedRowKeys.join(',')
          }
          deleteAction(this.url.delete, { ids: record }).then((resp) => {
              this.$message.success('删除成功!')
              this.$refs.table.refresh(true)
          })
      },
  },
  watch: {},
  activated () {
    if (store.getters.checkKeepAliveTmpList(this.$options.name)) {
      this.$refs.table.refresh(true)
    }
  },
  beforeRouteLeave (to, from, next) {
    if (!(to.name === 'userCashRecord' || to.name === 'userBalanceRecord')) {
      store.commit('noKeepAlive', this.$options.name)
    }
    next()
  },
  beforeRouteEnter (to, from, next) {
    if ((from.name === 'userCashRecord' || from.name === 'userBalanceRecord')) {
      store.commit('iskeepAliveTmp', 'UserList')
    }
    store.commit('iskeepAlive', 'UserList')
    next()
  }
}
</script>
<style scoped>
</style>
